package com.sgb.kjwl.view.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentLeaseCategoryBinding;
import com.sgb.kjwl.di.category.CategoryFragModule;
import com.sgb.kjwl.di.category.DaggerCategoryFragComponent;
import com.sgb.kjwl.view.ui.activity.lease.LeaseHallActivity2;
import com.sgb.kjwl.view.ui.adapter.category.CategoryLeaseLeftAdapter;
import com.sgb.kjwl.view.ui.adapter.category.CategoryLeaseRightAdapter;
import com.sgb.kjwl.viewmodel.category.CategoryLeaseFragmentModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.SectionAdapterOnItemClick;
import com.swgk.core.view.entity.MenuEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 租赁分类
 */
public class CategoryLeaseFragment extends AppFragment {
    @Inject
    public CategoryLeaseFragmentModel viewModel;
    private FragmentLeaseCategoryBinding binding;
    private CategoryLeaseLeftAdapter leftAdapter;
    private CategoryLeaseRightAdapter rightAdapter;
    private GridLayoutManager leftManager;
    private int position = 0;
    private int left = 0;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerCategoryFragComponent.builder()
                .appComponent(appComponent)
                .categoryFragModule(new CategoryFragModule(this))
                .build()
                .inject(this);
    }

    public static CategoryLeaseFragment newInstance() {
        return new CategoryLeaseFragment();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_lease_category, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        viewModel.getLeaseCategoryModel();
    }

    private void initView() {
        binding.actionBarTitle.setText("租赁分类");
        if (leftAdapter == null) {
            leftAdapter = new CategoryLeaseLeftAdapter(getActivity(), null);
        }
        binding.ivBack.setOnClickListener(this);

        //设置左侧适配器
        leftManager = new GridLayoutManager(getActivity(), 1);
        binding.leftRecycler.setLayoutManager(leftManager);
        binding.leftRecycler.setAdapter(leftAdapter);
//        binding.leftRecycler.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_EDEDED)));
        leftAdapter.setItemSelect(true);
        leftAdapter.setOnItemClick(new AdapterOnItemClick<MenuEntity<MenuEntity<MenuEntity>>>() {
            @Override
            public void onItemClick(MenuEntity<MenuEntity<MenuEntity>> menuEntity3MenuEntity3, int position) {
                showRightAdapter(menuEntity3MenuEntity3.getDatas());
                binding.rightRecycler.scrollToPosition(0);
                left = position;
            }
        });

        //设置右侧适配器
        if (rightAdapter == null) {
            rightAdapter = new CategoryLeaseRightAdapter(getActivity(), null);
        }
        binding.rightRecycler.setSectionedRecyclerViewAdapter(rightAdapter);
        binding.rightRecycler.setLoadingMoreEnabled(false);
        binding.rightRecycler.setPullRefreshEnabled(false);
        GridLayoutManager rightManager = new GridLayoutManager(getActivity(), 3);
        binding.rightRecycler.setLayoutManager(rightManager);
        binding.rightRecycler.setAdapter(rightAdapter);
        rightAdapter.setOnItemClickListener(new SectionAdapterOnItemClick<MenuEntity>() {
            @Override
            public void onItemClick(MenuEntity menuEntity3, int section, int position) {
                LeaseHallActivity2.start(getActivity(), 0, left + 1, section + 1, position + 1);
            }
        });
    }

    public void showCategoryView(List<MenuEntity<MenuEntity<MenuEntity>>> data) {
        if (data == null || data.size() <= position) {
            return;
        }
        setFactoryData(data);
        leftAdapter.setDatas(data);
        leftAdapter.setDefItem(position);
        leftManager.scrollToPosition(position);
        showRightAdapter(data.get(position).getDatas());
    }

    public void showRightAdapter(List<MenuEntity<MenuEntity>> data) {
        rightAdapter.setData(data);
    }

    private void setFactoryData(List<MenuEntity<MenuEntity<MenuEntity>>> data) {
        for (MenuEntity<MenuEntity<MenuEntity>> item : data) {
            if ("工厂租赁".equals(item.getName())) {
                List<MenuEntity<MenuEntity>> menuEntityList = item.getDatas();
                List<MenuEntity> list = new ArrayList<>();
                for (int i = 0; i < menuEntityList.size(); i++) {
                    list.add(menuEntityList.get(i));
                }
                menuEntityList.clear();
                menuEntityList.add(0, new MenuEntity<>());
                menuEntityList.get(0).setDatas(list);
            }
        }
    }

}